package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.OccupationDTO;
import com.example.springboot.entity.Department;
import com.example.springboot.entity.Occupation;
import com.example.springboot.entity.User;
import com.example.springboot.service.IDepartmentService;
import com.example.springboot.service.IOccupationService;
import com.example.springboot.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 岗位管理
 */
@RestController
@RequestMapping("/occupation")
@Slf4j
public class OccupationController {
    @Autowired
    private IOccupationService occupationService;

    @Autowired
    private IDepartmentService deptService;

    @Autowired
    private IUserService userService;

    private final String now = DateUtil.now();
    /**
     * 分页查询
     * @param name 查询岗位名称
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return
     */
    @GetMapping("/page")
    public Result save(@RequestParam(defaultValue = "") String name,
                       @RequestParam Integer pageNum,
                       @RequestParam Integer pageSize){
        QueryWrapper<Occupation> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("occupation_name", name);
        }

        // 获取分页数据
        IPage<Occupation> page = occupationService.page(new Page<>(pageNum, pageSize), queryWrapper);

        // 封装结果
        List<OccupationDTO> occupationDTOList = page.getRecords().stream().map(occupation -> {
            OccupationDTO occupationDTO = new OccupationDTO();
            occupationDTO.setId(occupation.getId());
            occupationDTO.setOccupationName(occupation.getOccupationName());
            occupationDTO.setDeptId(occupation.getDeptId());
            occupationDTO.setCreateTime(occupation.getCreateTime());
            occupationDTO.setFlag(occupation.getFlag());

            // 通过 dept_id 查询部门名称
            Department dept = deptService.getById(occupation.getDeptId());
            if (dept != null) {
                occupationDTO.setDepartmentName(dept.getDepartmentName());
            }

            return occupationDTO;
        }).collect(Collectors.toList());

        // 返回结果
        IPage<OccupationDTO> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(occupationDTOList);

        return Result.success(resultPage);
    }

    @GetMapping("/user/findById")
    public Result findById(@RequestParam int id) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("occupation_id", id);
        List<User> userList = userService.list(userQueryWrapper);
        return Result.success(userList);
    }

    @PostMapping()
    public Result save(@RequestBody Occupation occupation) {
        if (occupation.getId() == null) {
            occupation.setCreateTime(now);
        }
        occupationService.saveOrUpdate(occupation);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        occupationService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        occupationService.removeBatchByIds(ids);
        return Result.success("批量删除成功");
    }

    @GetMapping
    public Result findAll() {
        return Result.success(occupationService.list());
    }

}
